package com.baoxin.chenxixc4coach.dialog;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.MyCallback;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.TrainStudent;
import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.NetUtil;
import com.baoxin.chenxixc4coach.utils.TimeUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;

public class TestArrangeDialog extends BaseDialogFragment {

    private TextView mPlace;
    private TextView mTime;
    private Button mOk;
    private String[] places = {"场地A", "场地B", "场地C"};
    private TestArrangeDoneListener arrangeDoneListener;

    private Handler handler;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.arrange_test_dialog, null);
        //对话框背景设置为透明(否则背景无圆角)
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        handler = new MyHandler(this, activity) {
            @Override
            public void handleMessage(Message msg) {
                dismiss();

                if (msg.what == 0) {
                    arrangeDoneListener.onTestArrangeSucceed();
                } else {
                    arrangeDoneListener.onTestArrangeFailed();
                }
            }
        };
        mPlace = view.findViewById(R.id.test_place);
        mTime = view.findViewById(R.id.test_time);
        mOk = view.findViewById(R.id.ok);
        mTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                month += 1;
                                String timeYMD = year + "/" + month + "/" + dayOfMonth;
                                TimeUtil.StrToDate(timeYMD, "yyyy/MM/dd");
                                mTime.setText(timeYMD);
                            }
                        },
                        Calendar.getInstance().get(Calendar.YEAR),
                        Calendar.getInstance().get(Calendar.MONTH),
                        Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });
        mPlace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("选择考试场地");
                builder.setItems(places, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPlace.setText(places[which]);
                    }
                });
                builder.show();
            }
        });

        mOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTime.getText().toString().equals(getString(R.string.select_test_date))) {
                    return;
                }
                if (mPlace.getText().toString().equals(getString(R.string.select_test_place))) {
                    return;
                }

                ArrayList<TrainStudent> trainStudents = getArguments().getParcelableArrayList("trainStudents");

                test(trainStudents.get(0));

            }
        });
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        //宽度为match_parent，布局中无效
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout((int) (dm.widthPixels * 0.95), WindowManager.LayoutParams.WRAP_CONTENT);

    }

    public void setArrangeDoneListener(TestArrangeDoneListener arrangeDoneListener) {
        this.arrangeDoneListener = arrangeDoneListener;
    }

    public interface TestArrangeDoneListener {
        void onTestArrangeSucceed();

        void onTestArrangeFailed();
    }


    private void test(TrainStudent student) {

        JSONObject body = new JSONObject();
        try {
            body.put("serSubject", student.getTrState());
            body.put("serTrId", student.getTrId());
            body.put("serFeild", mPlace.getText().toString());
            body.put("serExamTime", mTime.getText().toString());

            NetUtil.post(activity, Interfaces.URL_ARRANGE_TEST, body.toString(), new MyCallback(Interfaces.URL_ARRANGE_TEST, handler));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
